<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :before-close="handlerClose"
    :visible.sync="visible"
    width="60%"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="所属分类" prop="quesCateId">
        <el-input placeholder="请选择分类" :value="current.name" type="text" readonly>
          <template slot="suffix">
            <el-button class="el-icon-edit" @click="checkSort" type="text">选择分类</el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="内容" prop="judgeqstInfo">
        <ck-editor ref="ckEditor" :change="contentChanged" />
      </el-form-item>
      <!--      <el-form-item label="答案"  prop="answ">-->
      <!--        <el-radio-group v-model="dataForm.answ">-->
      <!--          <el-radio :label="0">错</el-radio>-->
      <!--          <el-radio :label="1">对</el-radio>-->
      <!--        </el-radio-group>-->
      <!--      </el-form-item>-->
      <el-form-item label="答案" prop="answ">
        <el-radio-group v-model="dataForm.answ">
          <el-radio label="B">错</el-radio>
          <el-radio label="A">对</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="答案解析">
        <el-input v-model="dataForm.analysis" placeholder="答案解析"></el-input>
      </el-form-item>
      <el-form-item label="试题难度" prop="level">
        <el-radio-group v-model="dataForm.level">
          <el-radio :label="0">简单</el-radio>
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">困难</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handlerClose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </div>
    <!-- 分类管理 -->
    <el-dialog
      title="分类管理"
      :visible.sync="dialogFormVisible"
      :show-close="false"
      append-to-body
      width="30%"
    >
      <paper-cate
        ref="paperCate"
        v-if="paperCate"
        :setCurrent="setCurrent"
        :current="current"
        :url="{
                                update:'/exam-admin/ques/cate/update',
                                save:'/exam-admin/ques/cate/save',
                                delete:'/exam-admin/ques/cate/delete',
                                tree:'/exam-admin/ques/cate/tree'
                                }"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import paperCate from './../../../views/modules/paper/papercate'
import CkEditor from '../../../components/ckEditor/ckEditor'

export default {
  data() {
    return {
      dialogFormVisible: false,
      current: {},
      paperCate: false,
      visible: false,
      dataForm: {
        id: 0,
        info: '',
        cateId: '',
        answ: '',
        analysis: '',
        level: 0,
        questype: 3
      },
      loading: false,
      dataRule: {
        info: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        cateId: [
          { required: true, message: '题型号不能为空', trigger: 'blur' }
        ],
        answ: [
          {
            required: true,
            message: '答案（B：错，A：对）不能为空',
            trigger: 'blur'
          }
        ]
        // level: [
        //   {required: true, message: '试题难度（0-简单，1-普通，2-困难）不能为空', trigger: 'blur'}
        // ]
      }
    }
  },
  components: {
    CkEditor,
    paperCate
  },
  methods: {
    contentChanged(content) {
      this.dataForm.info = content
    },
    setCurrent(data) {
      if (data.id) {
        this.current = data
        this.dataForm.cateId = data.id
        this.$nextTick(_ => this.$refs['dataForm'].validateField('cateId'))
      }
    },
    checkSort() {
      this.dialogFormVisible = true
      this.paperCate = true
      this.$nextTick(() => {
        this.$refs.paperCate.init()
      })
    },
    init(id) {
      this.current = {
        name: '',
        id: ''
      }
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam-admin/ques/judge/info/${this.dataForm.id}`
            ),
            method: 'post'
          }).then(data => {
            if (data && data.code === 200) {
              this.$refs.ckEditor.setData(data.data.info)
              this.setCurrent({
                id: data.data.cateId,
                name: data.data.cateName
              })
              this.dataForm.answ = data.data.answ
              this.dataForm.analysis = data.data.analysis
              this.dataForm.level = data.data.level
              this.dataForm.info = data.data.info
            }
          })
        } else {
          this.dataForm.answ = ''
          this.dataForm.analysis = ''
          this.dataForm.level = 0
          this.dataForm.info = ''
          this.$refs.ckEditor.setData('')
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.loading = true
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam-admin/ques/judge/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: {
              id: this.dataForm.id || undefined,
              info: this.dataForm.info,
              cateId: this.dataForm.cateId,
              answ: this.dataForm.answ,
              analysis: this.dataForm.analysis,
              level: this.dataForm.level,
              quesType: this.dataForm.questype
            }
          })
            .then(data => {
              if (data && data.code === 200) {
                this.$notify.success({
                  title: '提示',
                  message: '操作成功'
                })
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$notify.error({
                  title: '提示',
                  message: data.msg
                })
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    handlerClose() {
      this.visible = false
    }
  }
}
</script>
